package cn.tedu.medicalpath.prescription.service.impl;

import cn.tedu.medicalpath.medicine.mapper.MedicineMapper;
import cn.tedu.medicalpath.medicine.pojo.vo.MedicineVO;
import cn.tedu.medicalpath.prescription.mapper.PrescriptionMapper;
import cn.tedu.medicalpath.prescription.mapper.PrescriptionMedicineMapper;
import cn.tedu.medicalpath.prescription.pojo.dto.PrescriptionMedicineQuery;
import cn.tedu.medicalpath.prescription.pojo.dto.PrescriptionMedicineSaveParam;
import cn.tedu.medicalpath.prescription.pojo.entity.Prescription;
import cn.tedu.medicalpath.prescription.pojo.entity.PrescriptionMedicine;
import cn.tedu.medicalpath.prescription.pojo.vo.PrescriptionMedicineVO;
import cn.tedu.medicalpath.prescription.service.PrescriptionMedicineService;
import cn.tedu.medicalpath.prescription.service.PrescriptionService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.Collections;
import java.util.Date;
import java.util.List;

/**
 * 处方单药品的service实现类
 *现有处方单，后有处方表
 */
@Slf4j
@Service
public class PrescriptionMedicineServiceImpl implements PrescriptionMedicineService {
    @Autowired
    PrescriptionMedicineMapper prescriptionMedicineMapper;
    @Autowired
    MedicineMapper medicineMapper;
    @Autowired
    PrescriptionMapper prescriptionMapper;


    // 保存处方单药品
    @Transactional(propagation = Propagation.REQUIRED)
    @Override
    public void savePrescriptionMedicine(PrescriptionMedicineSaveParam prescriptionMedicineSaveParam) {
        log.debug("开始处理-【保存处方单药品】业务-入参：{}",prescriptionMedicineSaveParam);
        PrescriptionMedicine prescriptionMedicine=new PrescriptionMedicine();
        BeanUtils.copyProperties(prescriptionMedicineSaveParam,prescriptionMedicine);
        //计算药品价格
        MedicineVO medicineVO = medicineMapper.selectMedicinePrice(prescriptionMedicineSaveParam.getMedicineId());
        if (medicineVO.getPrice() == null || prescriptionMedicineSaveParam.getNumber() == null) {
            throw new RuntimeException("药品价格或数量不能为空");
        }
        int price = medicineVO.getPrice() * prescriptionMedicineSaveParam.getNumber();
        int insurancePrice = price;
        if (medicineVO.getInsuranceCode()!=null){
            //计算医保结算金额
            //todo 医保结算金额比例
            insurancePrice = price * 8 / 10;
        }
        prescriptionMedicine.setPrice(price);
        if (prescriptionMedicineSaveParam.getInsurancePrice()==null){
            //保存业务
            prescriptionMedicine.setInsurancePrice(insurancePrice);
            log.debug("处理中-【保存处方单药品】业务-入参：{}",prescriptionMedicine);
            prescriptionMedicineMapper.savePrescriptionMedicine(prescriptionMedicine);
            updatePrescriptionPrice(prescriptionMedicineSaveParam.getPrescriptionId());
            log.debug("处理成功-【保存处方单药品】业务-入参：{}",prescriptionMedicine);
            //todo 修改药品库存
        }else {
            //修改业务
            prescriptionMedicine.setInsurancePrice(insurancePrice);
            log.debug("修改中-【保存处方单药品】业务-入参：{}",prescriptionMedicine);
            prescriptionMedicineMapper.updatePrescriptionMedicine(prescriptionMedicine);
            updatePrescriptionPrice(prescriptionMedicineSaveParam.getPrescriptionId());
            log.debug("修改成功-【保存处方单药品】业务-入参：{}",prescriptionMedicine);
        }
    }

    public void updatePrescriptionPrice(Long prescriptionId){
        log.debug("【更新处方单金额】-入参：处方单id：{}",prescriptionId);
        PrescriptionMedicineQuery prescriptionMedicineQuery = new PrescriptionMedicineQuery();
        prescriptionMedicineQuery.setPrescriptionId(prescriptionId);
        //查询所有的处方单药品
        List<PrescriptionMedicineVO> prescriptionMedicineVOS =
                prescriptionMedicineMapper.selectPrescriptionMedicine(prescriptionMedicineQuery);
        //遍历 所有的处方单药品 累加金额
        Integer price = 0;
        Integer insurancePrice = 0;
        if (!prescriptionMedicineVOS.isEmpty()) {
            for (PrescriptionMedicineVO prescriptionMedicineVO : prescriptionMedicineVOS) {
                log.debug("【更新处方单金额】-累加处方单药品金额：处方单药品：{}",prescriptionMedicineVO);
                price += prescriptionMedicineVO.getPrice();
                insurancePrice += prescriptionMedicineVO.getInsurancePrice();
            }
        }
        log.debug("【更新处方单金额】-累加处方单药品金额-累加后：金额：{}，医保结算金额：{}",price,insurancePrice);
        //将数据存入数据库中
        Prescription prescription = new Prescription();
        prescription.setId(prescriptionId);
        prescription.setPrice(price);
        prescription.setInsurancePrice(insurancePrice);
        prescription.setUpdateTime(new Date());
        log.debug("【更新处方单金额】-将新价格保存入数据库-入参：{}",prescription);
        prescriptionMapper.savePrescription(prescription);
        log.debug("【更新处方单金额】-将新价格保存入数据库-成功-入参：{}",prescription);
    }

    /**
     * 修改处方单药品
     */
    /*@Transactional(propagation = Propagation.REQUIRED)
    @Override
    public void updatePrescriptionMedicine(PrescriptionMedicineSaveParam prescriptionMedicineSaveParam) {
        log.debug("开始处理-【修改处方单药品】业务-入参：{}",prescriptionMedicineSaveParam);
        PrescriptionMedicine prescriptionMedicine=new PrescriptionMedicine();
        BeanUtils.copyProperties(prescriptionMedicineSaveParam,prescriptionMedicine);
        //计算药品价格
        //从数据库查询出价格
        MedicineVO medicineVO = medicineMapper.selectMedicinePrice(prescriptionMedicineSaveParam.getMedicineId());
        if (medicineVO.getPrice() == null || prescriptionMedicineSaveParam.getNumber() == null) {
            throw new RuntimeException("药品价格或数量不能为空");
        }
        int price = medicineVO.getPrice() * prescriptionMedicineSaveParam.getNumber();
        int insurancePrice = price;
        if (medicineVO.getInsuranceCode()!=null){
            //计算医保结算金额
            //todo 医保结算金额比例
            insurancePrice = price * 8 / 10;
        }
        prescriptionMedicine.setPrice(price);
        prescriptionMedicine.setInsurancePrice(insurancePrice);
        prescriptionMedicineMapper.updatePrescriptionMedicine(prescriptionMedicine);
        updatePrescriptionPrice(prescriptionMedicineSaveParam.getPrescriptionId());
    }*/

    /**
     * 查找处方单药品
     * @param prescriptionMedicineQuery
     * @return
     */
    @Override
    public List<PrescriptionMedicineVO> selectPrescriptionMedicine(PrescriptionMedicineQuery prescriptionMedicineQuery) {
        log.debug("开始处理-【查询处方单药品】业务-入参：{}",prescriptionMedicineQuery);
        List<PrescriptionMedicineVO> list=prescriptionMedicineMapper.selectPrescriptionMedicine(prescriptionMedicineQuery);
        log.debug("查询处理结果-【查询处方单药品】业务-入参：{},出参：{}",prescriptionMedicineQuery,list);
        return list;
    }

    @Transactional(propagation = Propagation.REQUIRED)
    @Override
    public void deletePrescriptionMedicine(PrescriptionMedicineQuery prescriptionMedicineQuery) {
        log.debug("开始处理-【删除处方单药品】业务-入参：{}",prescriptionMedicineQuery);
        prescriptionMedicineMapper.deletePrescriptionMedicine(prescriptionMedicineQuery);
        updatePrescriptionPrice(prescriptionMedicineQuery.getPrescriptionId());
    }
}
